<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="学号" prop="studentid">
      <el-input v-model="dataForm.studentid" placeholder="学号"></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="studentname">
      <el-input v-model="dataForm.studentname" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-input v-model="dataForm.sex" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="年级" prop="grade">
      <el-input v-model="dataForm.grade" placeholder="年级"></el-input>
    </el-form-item>
    <el-form-item label="专业" prop="major">
      <el-input v-model="dataForm.major" placeholder="专业"></el-input>
    </el-form-item>
    <el-form-item label="班级" prop="clazz">
      <el-input v-model="dataForm.clazz" placeholder="班级"></el-input>
    </el-form-item>
    <el-form-item label="学院" prop="institute">
      <el-input v-model="dataForm.institute" placeholder="学院"></el-input>
    </el-form-item>
    <el-form-item label="电话号码" prop="tel">
      <el-input v-model="dataForm.tel" placeholder="电话号码"></el-input>
    </el-form-item>
    <el-form-item label="电子邮件" prop="email">
      <el-input v-model="dataForm.email" placeholder="电子邮件"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="pwd">
      <el-input v-model="dataForm.pwd" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="身份证号" prop="cardid">
      <el-input v-model="dataForm.cardid" placeholder="身份证号"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          studentid: '',
          studentname: '',
          sex: '',
          grade: '',
          major: '',
          clazz: '',
          institute: '',
          tel: '',
          email: '',
          pwd: '',
          cardid: ''
        },
        dataRule: {
          studentid: [
            { required: true, message: '学号不能为空', trigger: 'blur' }
          ],
          studentname: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          grade: [
            { required: true, message: '年级不能为空', trigger: 'blur' }
          ],
          major: [
            { required: true, message: '专业不能为空', trigger: 'blur' }
          ],
          clazz: [
            { required: true, message: '班级不能为空', trigger: 'blur' }
          ],
          institute: [
            { required: true, message: '学院不能为空', trigger: 'blur' }
          ],
          tel: [
            { required: true, message: '电话号码不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '电子邮件不能为空', trigger: 'blur' }
          ],
          pwd: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          cardid: [
            { required: true, message: '身份证号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/exam/student/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.studentid = data.student.studentid
                this.dataForm.studentname = data.student.studentname
                this.dataForm.sex = data.student.sex
                this.dataForm.grade = data.student.grade
                this.dataForm.major = data.student.major
                this.dataForm.clazz = data.student.clazz
                this.dataForm.institute = data.student.institute
                this.dataForm.tel = data.student.tel
                this.dataForm.email = data.student.email
                this.dataForm.pwd = data.student.pwd
                this.dataForm.cardid = data.student.cardid
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/exam/student/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'studentid': this.dataForm.studentid,
                'studentname': this.dataForm.studentname,
                'sex': this.dataForm.sex,
                'grade': this.dataForm.grade,
                'major': this.dataForm.major,
                'clazz': this.dataForm.clazz,
                'institute': this.dataForm.institute,
                'tel': this.dataForm.tel,
                'email': this.dataForm.email,
                'pwd': this.dataForm.pwd,
                'cardid': this.dataForm.cardid
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
